package com.vn.rainbow.exoalarm.ui;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.content.Context;
import android.content.IntentFilter;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vn.rainbow.exoalarm.R;
import com.vn.rainbow.exoalarm.listener.TickListener;

/**
 * TODO: document your custom view class.
 */
public class TimeView extends RelativeLayout implements TickListener {

	private View timeLayout;
	private TextView tvDate, tvTime, tvAm;
	private Context mContext;
	private LinearLayout llBigTime, llSmallTime, llAlarm;

	public static SimpleDateFormat date_format = new SimpleDateFormat(
			"EEE, MMM dd", Locale.getDefault());

	public static SimpleDateFormat date_vn_format = new SimpleDateFormat(
			"EEE, dd MMM", Locale.getDefault());

	private boolean is24HourFormat;

	private void setTime() {

		tvDate.setText(getDate());
		tvTime.setText(getTime());
		tvAm.setText(getAM());

	}

	public String getTime() {

		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

		if (!is24HourFormat && hour > 12) {
			hour -= 12;
		}

		if (!is24HourFormat && hour == 0)
			hour = 12;

		String str1 = Integer.toString(hour);
		if (str1.length() == 1)
			str1 = "0" + str1;

		String str2 = Integer.toString(Calendar.getInstance().get(
				Calendar.MINUTE));
		if (str2.length() == 1)
			str2 = "0" + str2;
		return str1 + ":" + str2;
	}

	public String getAM() {
		if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 12) {
			return "AM";
		}
		return "PM";
	}

	public String getDate() {

		Calendar mCalendar = Calendar.getInstance();

		String local = Locale.getDefault().toString();
		String date;
		if (local != null && "vi_VN".equals(local)) {
			date = date_vn_format.format(mCalendar.getTimeInMillis());
		} else {
			date = date_format.format(mCalendar.getTimeInMillis());
			date = date.substring(0, 1).toUpperCase() + date.substring(1);
		}

		return date;
	}

	public TimeView(Context context, AttributeSet attr) {
		super(context, attr);
		mContext = context;

		timeLayout = View.inflate(context, R.layout.layout_module_launchtime,
				this);

		is24HourFormat = DateFormat.is24HourFormat(mContext);

		initViews();

		setTime();
	}

	private void initViews() {

		tvDate = (TextView) timeLayout.findViewById(R.id.tvDate);
		tvTime = (TextView) timeLayout.findViewById(R.id.tvTime);
		tvAm = (TextView) timeLayout.findViewById(R.id.tv_am);
		if (is24HourFormat)
			tvAm.setVisibility(View.GONE);
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();

	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();

	}

	@Override
	public void onStick() {
		// TODO Auto-generated method stub
		setTime();
	}

}
